<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title>Simulations In One Dimension Example (k-Wave)</title>
	<link rel="stylesheet" href="kwavehelpstyle.css" type="text/css">
	<meta name="description" content="Simulations In One Dimension Example.">
</head>

<body><div class="content">

<h1>Simulations In One Dimension Example</h1>

<p>This example provides a simple demonstration of using k-Wave for the simulation and detection of the pressure field generated by an initial pressure distribution within a one-dimensional heterogeneous propagation medium. It builds on the <a href="example_ivp_homogeneous_medium.html">Homogeneous Propagation Medium</a> and <a href="example_ivp_heterogeneous_medium.html">Heterogeneous Propagation Medium</a> examples.</p>

<p>
    <ul>
        <li><a href="matlab:edit([getkWavePath('examples') 'example_ivp_1D_simulation.m']);" target="_top">Open the file in the MATLAB Editor</a></li>
        <li><a href="matlab:run([getkWavePath('examples') 'example_ivp_1D_simulation']);" target="_top">Run the file in MATLAB</a></li>
    </ul>
</p>

<h2>Contents</h2>
<div>
	<ul>
        <li><a href="#heading2">Creating the k-space grid and defining the medium properties</a></li>
        <li><a href="#heading3">Defining the initial pressure distribution and sensor mask</a></li>
        <li><a href="#heading4">Running the simulation</a></li>
    </ul>
</div>

<a name="heading2"></a>
<h2>Creating the k-space grid and defining the medium properties</h2>

<p>Simulations in one-dimension are performed in an analogous fashion to those in two-dimensions. The medium discretisation is again performed by <code><a href="kWaveGrid.html">kWaveGrid</a></code> using the inputs for a single dimension. The properties of a heterogeneous acoustic propagation medium are also given as one-dimensional column vectors.</p>

<pre class="codeinput">
<span class="comment">% create the computational grid</span>
Nx = 512;       <span class="comment">% number of grid points in the x (row) direction</span>
dx = 0.05e-3;   <span class="comment">% grid point spacing in the x direction [m]</span>
kgrid = kWaveGrid(Nx, dx);

<span class="comment">% define the properties of the propagation medium</span>
medium.sound_speed = 1500 * ones(Nx, 1);    <span class="comment">% [m/s]</span>
medium.sound_speed(1:round(Nx/3)) = 2000;   <span class="comment">% [m/s]</span>
medium.density = 1000 * ones(Nx, 1);        <span class="comment">% [kg/m^3]</span>
medium.density(round(4*Nx/5):end) = 1500;   <span class="comment">% [kg/m^3]</span>
</pre>

<a name="heading3"></a>
<h2>Defining the initial pressure distribution and sensor mask</h2>

<p>As in two-dimensions, the initial pressure distribution is set using a vector which contains the initial pressure values for each grid point within the computational domain. Here a smoothly varying pressure function is defined using a portion of a sinusoid.</p>

<pre class="codeinput">
<span class="comment">% create initial pressure distribution using a smoothly shaped sinusoid</span>
x_pos = 280;    <span class="comment">% [grid points]</span>
width = 100;    <span class="comment">% [grid points]</span>
height = 1;     <span class="comment">% [Pa]</span>
in = (0:pi/(width/2):2*pi).';
source.p0 = [zeros(x_pos, 1); ((height/2) * sin(in - pi/2) + (height/2)); zeros(Nx - x_pos  - width - 1, 1)];
</pre>

<p>Again the sensor mask, which defines the locations where the pressure field is recorded at each time-step, can be given as a list of Cartesian coordinates, a binary mask, or the grid coordinates of two opposing ends of a line. In this example, a Cartesian sensor mask with two points is defined.</p>

<pre class="codeinput">
<span class="comment">% create a Cartesian sensor mask</span>
sensor.mask = [-10e-3, 10e-3];  <span class="comment">% [mm]</span>
</pre>

<a name="heading4"></a>
<h2>Running the simulation</h2>

<p>The computation is started by passing the four input structures, <code>kgrid</code>, <code>medium</code>, <code>source</code>, and <code>sensor</code> to <code><a href="kspaceFirstOrder1D.html">kspaceFirstOrder1D</a></code>. To record long enough to capture the reflections from the heterogeneous interfaces, the time sampling is defined using the <code>makeTime</code> method of the <code><a href="kWaveGrid.html">kWaveGrid</a></code> class. By default, a visualisation of the propagating wave-field and a status bar are displayed.</p>

<pre class="codeinput">
<span class="comment">% set the simulation time to capture the reflections</span>
t_end = 2.5 * kgrid.x_size / max(medium.sound_speed(:));

<span class="comment">% define the time array</span>
kgrid.makeTime(medium.sound_speed, [], t_end);

<span class="comment">% run the simulation</span>
sensor_data = kspaceFirstOrder1D(kgrid, medium, source, sensor, 'PlotLayout', true);
</pre>

<p>As the function runs, status updates and computational parameters are printed to the command line.</p>

<pre class="codeinput">
Running k-Wave simulation...
  start time: 13-Apr-2017 21:49:35
  reference sound speed: 2000m/s
  dt: 7.5ns, t_end: 31.995us, time steps: 4267
  input grid size: 512 grid points (25.6mm)
  maximum supported frequency: 15MHz
  smoothing p0 distribution...
  precomputation completed in 0.53842s
  starting time loop...
  estimated simulation time 4.011s...
  simulation completed in 4.0106s
  total computation time 4.618s
</pre>

<p>A plot of the initial pressure distribution, sensor mask, and medium properties (returned using <code>'PlotLayout'</code> set to <code>true</code>) is given below.</p>

<img vspace="5" hspace="5" src="images/example_ivp_1D_simulation_01.png" style="width:560px;height:420px;" alt="">

<p>When the time loop has completed, the function returns the recorded time series at each of sensor points defined by <code>sensor_mask</code>. The ordering is again dependent on whether a Cartesian or binary sensor mask is used. A visualisation of the recorded sensor data is given below.</p>

<img vspace="5" hspace="5" src="images/example_ivp_1D_simulation_02.png" style="width:560px;height:420px;" alt="">

<p>Note, in some cases the animated visualisation can significantly slow down the simulation. The computational speed can be increased by switching off the animations by setting the optional input <code>'PlotSim'</code> to <code>false</code>.

</div></body></html>